package com.jxtti.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.Attach;
import com.jxtti.entity.SiteAd;
import com.jxtti.service.SiteAdService;
import com.jxtti.utils.CommConstant;
import com.jxtti.utils.ExceptionUtil;
import com.jxtti.utils.FileOperate;
/**
 * 
 * <br>
 * <b>功能：</b>SiteAdController<br>
 * <b>作者：lwb</b><br>
 * <b>日期：</b> Nov 26, 2015 <br>
 * <b>版权所有：<b>版权所有(C) 2015，<br>
 */ 
@Controller
@RequestMapping("/siteAd") 
public class SiteAdController {
	
	private final static Logger log= Logger.getLogger(SiteAdController.class);
	
	// Servrice start
	@Autowired
	private SiteAdService  siteAdService; 
	
	
	
	
	
	/**
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list") 
	public ModelAndView  list(@RequestParam Map<String, Object> data,  SiteAd siteAd, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {			
			data =  siteAdService.list(data, siteAd);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/SiteManager/site_ad_list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	/**
	 * 获取数据.
	 * @param data
	 * @param siteAd
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listData") 
	@ResponseBody
	public Map<String, Object> listData(@RequestParam Map<String, Object> data,  SiteAd siteAd, HttpServletRequest request,
			HttpServletResponse response) throws Exception{	
		
		Map<String, Object> result = new HashMap<String, Object>();		
		
		PageInfo<SiteAd> page = siteAdService.queryAll(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/SiteManager/site_ad_add.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SiteAd  siteAd, HttpServletRequest request,
			HttpServletResponse response) throws Exception{

		ModelAndView mav = new ModelAndView();
		try {
			// 保存数据
			List<Attach> lst = FileOperate.savefile(response, request, CommConstant.ATTACH_FILE);
			if(lst.size() > 0)
			{
				Attach attach = lst.get(0);
				siteAd.setFilename(attach.getAttach_name());
				siteAd.setFilepath(attach.getAttach_path());
			}
			
			int exeCount = siteAdService.add(siteAd);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, SiteAd  siteAd,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			siteAd = siteAdService.querySiteAdById(siteAd);
		
			data.put("siteAd", siteAd);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/SiteManager/site_ad_edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	
	
	
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SiteAd  siteAd, HttpServletRequest request,
			HttpServletResponse response) {
		try {			
			int exeCount;
			if(data.get("type")!=null&&data.get("type").equals("upDown")){ //上移下移
				
				SiteAd siteAdTem=siteAdService.querySiteAdById(siteAd);
				siteAdTem.setSortid(siteAd.getSortid());
				exeCount = siteAdService.update(siteAdTem);
				
			}else{
				// 修改
				SiteAd siteAdTem=siteAdService.querySiteAdById(siteAd);
				List<Attach> lst = FileOperate.savefile(response, request, CommConstant.ATTACH_FILE);
				if(lst.size() > 0)
				{
					Attach attach = lst.get(0);
					siteAd.setFilename(attach.getAttach_name());
					siteAd.setFilepath(attach.getAttach_path());
					siteAd.setFilesize(attach.getAttach_size());
				}else
				{
					siteAd.setFilename(siteAdTem.getFilename());
					siteAd.setFilepath(siteAdTem.getFilepath());
					siteAd.setFilesize(siteAdTem.getFilesize());
				}				
				exeCount = siteAdService.update( siteAd);
			}
			
			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	

/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			SiteAd  siteAd, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			 siteAd=siteAdService.querySiteAdById( siteAd);
			int exeCount = siteAdService.delete(siteAd);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}

}
